local BagLogic = require("bag.BagLogic")
local ItemLogic = require("bag.ItemLogic")
local Msg = require("core.Msg")
local EquipLogic = require("bag.EquipLogic")
local ExcelItem = require("excel.Item")
local ExtendBoxConf = ExcelItem.extendBox
local GridLogic = require("bag.GridLogic")
local CombatCalc = require("skill.CombatCalc")
local Grid = require("bag.Grid")
local ShenbingLogic = require("shenbing.ShenbingLogic")
local ObjHuman = require("core.ObjHuman")
local RoleFFI = require("role.RoleFFI")
local Buffer = require("buffer.Buffer")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local Obj = require("core.Obj")
local Status = require("scene.Status")
local HorseLogic = require("horse.HorseLogic")
local TaskCallbacks = require("task.TaskCallbacks")
local ItemDefine = require("bag.ItemDefine")
local VipLogic = require("vip.VipLogic")
local PetLogic = require("pet.PetLogic")
local BaowuLogic = require("baowu.BaowuLogic")
local sbMainConfig = require("excel.Shenbing").shenbing
local RoleAttr = require("role.RoleAttr")
local PetConfig = require("excel.Pet").pet
local AtticConfig = require("excel.Pet").petAttic
local BaowuConfig = require("excel.Baowu").baowu
local HorseStageConfig = require("excel.Horse").stage
local HorseHuanhuaConfig = require("excel.Horse").huanhua
local ArmourHandler = require("armour.HandlerL")
local TitleLogic = require("title.TitleLogic")

function setHero(other, info)
    info.name = other.db.name
    info.uuid = other.db._id
    info.sbEffect = ShenbingLogic.getPutonSbIcon(other)
    info.sbEffect = info.sbEffect or 0
    info.titleName = TitleLogic.getTitleName(other)

    Status.makeViewInfo(info.viewInfo, other.db.sex, Obj.getBodyInfo(other))
    local index = 0
    for i=0, 13 do
        local grid = other.db.equip[i]
        if grid ~= nil and grid ~= 0 then
            index = index + 1
            Grid.makeItem(other.db.equip[i], info.equips[index], 0, other)
        end
    end
    info.equips[0] = index
    index = 0
    for k, v in pairs(other.attr) do
        index = index + 1
        info.roleAttr[index].key = k
        info.roleAttr[index].value = v
    end
    info.roleAttr[0] = index
    info.vipLv = VipLogic.getVipLv(other)
    info.official = 0--db.official
    info.familyName = other.db.familyName
end

function setHorse(other, info)
    local len = 0
    for i=1, #HorseStageConfig do
        len = len + 1
        HorseLogic.setHorseInfo(other, i, info.horse[i], true)
    end
    for k, v in pairs(HorseHuanhuaConfig) do
        len = len + 1
        HorseLogic.setHorseInfo(other, k, info.horse[len], true)
    end
    info.horse[0] = len
    info.stageLv = other.db.horseStageLv
    info.starLv = other.db.horseStarLv
    HorseLogic.calCurAddAttr(other, info.horseAttr)
    info.horsePower = RoleAttr.calPowerProto(info.horseAttr)
end

function setShenbing(other, info)
    info.shenbing[0] = #sbMainConfig
    for i = 1, #sbMainConfig do
        ShenbingLogic.setShenbingInfo(other, i, info.shenbing[i], true)
    end
    local attr = ShenbingLogic.getAllAttr(other)
    local len = 0
    for k, v in pairs(attr) do
        len = len +1
        info.sbAllAttr[len].key = k
        info.sbAllAttr[len].value = v
    end
    info.sbAllAttr[0] = len
end

function setPet(other, info)
    info.pet[0] = #PetConfig
    for i=1, #PetConfig do
        PetLogic.setPetInfo(other, info.pet[i], i, true)
    end
    info.petAllAttr[0] = 0
    for i = 0, other.db.petLen - 1 do
        PetLogic.calcRA(other, other.db.pet[i].id, info.petAllAttr, nil, i)
    end
    for i=1, info.petAllAttr[0] do
        info.petAllAttr[i].value = info.petAllAttr[i].value * (other.db.petAtticLv > 0 and (AtticConfig[other.db.petAtticLv].addPer/100 + 1) or 1)
    end
    info.petAllPower = RoleAttr.calPowerProto(info.petAllAttr)
end

function setBaowu(other, info)
    info.baowu[0] = #BaowuConfig
    for i=1, #BaowuConfig do
        BaowuLogic.setBaowuInfo(other, i, info.baowu[i], true)
    end
    info.baowuAllAttr[0] = 0
    BaowuLogic.getAllAttr(other, info.baowuAllAttr)
    info.baowuAllPower = RoleAttr.calPowerProto(info.baowuAllAttr) + BaowuLogic.getBaowuSkillPower(other)
end

function OnSearch(human, oType, db)
    local other = {}
    other.obj_type = Obj.TYPE_HUMAN
    other.db = db
    other.attr = {}
    Buffer.init(other)
    CombatCalc.initAttr(other)
    ObjHuman.doCalc(other, true)
    local sendMsg = Msg.gc.GC_SEARCH_OHTER
    sendMsg.hero[0] = 0
    sendMsg.horse[0] = 0
    sendMsg.shenbing[0] = 0
    sendMsg.pet[0] = 0
    sendMsg.baowu[0] = 0
    sendMsg.armour[0] = 0
    if db.type == 1 then
        sendMsg.hero[0] = 1
        setHero(other, sendMsg.hero[1])
    elseif db.type == 2 then
        sendMsg.horse[0] = 1
        setHorse(other, sendMsg.horse[1])
    elseif db.type == 3 then
        sendMsg.shenbing[0] = 1
        setShenbing(other, sendMsg.shenbing[1])
    elseif db.type == 4 then
        sendMsg.pet[0] = 1
        setPet(other, sendMsg.pet[1])
    elseif db.type == 5 then
        sendMsg.baowu[0] = 1
        setBaowu(other, sendMsg.baowu[1])
    elseif db.type == 6 then
        if other then
            sendMsg.armour[0] = 1
            ArmourHandler.warpSearch(sendMsg.armour[1], other)
        end
    end
    Msg.send(sendMsg, human.fd)
end




